﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Collections.ObjectModel;
using TwitterBEIS_SL3_SLApp.TwitterProxyReference;

namespace TwitterBEIS_SL3_SLApp
{
    public enum PageType { UPdates, Replies, DMs, Sent, Friends };
    public partial class MainUI : UserControl
    {
        private PageType pagetype;
        public PageType Pagetype { get { return pagetype; } }

        public MainUI(PageType type)
        {
            InitializeComponent();
            pagetype = type;
            Storage.ReadSecret();

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 5, 0);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        static void timer_Tick(object sender, EventArgs e)
        {
            if(NetworkInterface.GetIsNetworkAvailable())
                Storage.SyncData();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Init();
        }

        public void Init()
        {
            try
            {
                Tweet.Text = "";
                CharsLeft.Text = "140";
                Update.IsEnabled = false;

                switch(pagetype)
                {
                    case PageType.UPdates:
                        Storage.SetFriendsTimeLine(this);
                        break;
                     case PageType.Replies:
                        Storage.SetReplies(this);
                        break;
                    case PageType.DMs:
                        Storage.SetDMs(this);
                        break;
                    case PageType.Sent:
                        Storage.SetSent(this);
                        break;
                    case PageType.Friends:
                        Storage.SetFriends(this);
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

         private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (Tweet.Text.Length > 140)
            {
                CharsLeft.Foreground = new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0, 0));
                CharsLeft.Text = (140 - Tweet.Text.Length).ToString();
                Update.IsEnabled = false;
            }
            else
            {
                CharsLeft.Foreground = new SolidColorBrush(Color.FromArgb(0xff, 0x80, 0x80, 0x80));
                CharsLeft.Text = (140 - Tweet.Text.Length).ToString();
                Update.IsEnabled = true;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Storage.Update(this, Tweet.Text);
        }

        public void Warning(string text)
        {
            Info_TBK.Style = App.Current.Resources["ErrorMessageStyle"] as Style;
            Info_TBK.Text = text;
        }
    }
}
